/*
 * @Author: fuhongliang-TSJ hongliang.fu@4d-bios.com
 * @Date: 2025-10-13 10:59:48
 * @LastEditTime: 2025-10-14 16:38:36
 * @LastEditors: fuhongliang-TSJ hongliang.fu@4d-bios.com
 * @Description: 
 * @FilePath: \lc_-esp32_-s3\main\lc\handheld\comp_app.c
 * 立创实战派ESP32-S3开发板
 * Copyright (c) 2025 by 4D Bios, All Rights Reserved. 
 */

#include "handheld.h"
#include "rng.h"
#include "bsp_lvgl.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"

extern lv_obj_t *icon_in_obj; // 应用界面
extern int icon_flag;

static lv_obj_t *label_comp;
static lv_obj_t *meter_compass;
static lv_meter_scale_t *scale_compass;

static lv_timer_t *my_lv_timer;

/**
 * @description: 返回主界面向上滑动事件处理函数
 * @param {lv_event_t} *e
 * @return {*}
 */
static void my_gesture_event_cb(lv_event_t *e)
{
    lv_dir_t dir = lv_indev_get_gesture_dir(lv_indev_get_act());
    if (dir == LV_DIR_TOP)
    {
        lv_obj_del(icon_in_obj); //删除画布
        lv_timer_del(my_lv_timer);
        icon_flag = 0;
    }
}

/**
 * @description:  定时更新方位角的值
 * @param {lv_timer_t *} timer
 * @return {*}
 */
static void comp_update_cb(lv_timer_t * timer)
{
    int comp_angle = rng_get_random_range(0, 360);    //76-81随机数，虚拟湿度值

    lvgl_port_lock(0);
    
    lv_label_set_text_fmt(label_comp, "%d°", comp_angle);
    lv_meter_set_scale_range(meter_compass, scale_compass, 0, 360, 360, comp_angle); 

    lvgl_port_unlock();
}

/**
 * @description: 指南针应用
 * @return {*}
 */
void comp_app(void)
{
    lvgl_port_lock(0);

    static lv_style_t style;
    lv_style_init(&style);
    lv_style_set_radius(&style, 10);
    lv_style_set_opa(&style, LV_OPA_COVER);
    lv_style_set_bg_color(&style, lv_color_hex(0x00BFFF));
    lv_style_set_bg_grad_color(&style, lv_color_hex(0x00BF00));
    lv_style_set_bg_grad_dir(&style, LV_GRAD_DIR_VER);
    lv_style_set_border_width(&style, 0);
    lv_style_set_pad_all(&style, 0);
    lv_style_set_width(&style, 320);
    lv_style_set_height(&style, 240);
    
    // 创建一个界面
    icon_in_obj = lv_obj_create(lv_scr_act());
    lv_obj_add_style(icon_in_obj, &style, 0);

    // 仪表部件
    meter_compass = lv_meter_create(icon_in_obj);
    lv_obj_center(meter_compass);
    lv_obj_set_size(meter_compass, 220, 220);
    lv_obj_set_style_bg_opa(meter_compass, LV_OPA_TRANSP, 0);
    lv_obj_remove_style(meter_compass, NULL, LV_PART_INDICATOR);
    lv_obj_set_style_text_color(meter_compass, lv_color_hex(0xffffff),0);

    // 刻度
    scale_compass = lv_meter_add_scale(meter_compass);
    lv_meter_set_scale_ticks(meter_compass, scale_compass, 61, 1, 10, lv_color_hex(0xffffff)); /* 设置小刻度 */
    lv_meter_set_scale_major_ticks(meter_compass, scale_compass, 10, 2, 16, lv_color_hex(0xffffff), 10); /* 设置主刻度 */    
    lv_meter_set_scale_range(meter_compass, scale_compass, 0, 360, 360, 270); /* 设置仪表数值范围、有效角度和旋转角度 */

    // 添加指针
    lv_obj_t *arrow_label = lv_label_create(icon_in_obj);
    lv_label_set_text(arrow_label, LV_SYMBOL_DOWN);
    lv_obj_set_style_text_color(arrow_label, lv_palette_main(LV_PALETTE_RED), 0);
    lv_obj_align(arrow_label, LV_ALIGN_CENTER, 0, -100);

    // 显示方位角度
    label_comp = lv_label_create(icon_in_obj);
    lv_obj_set_style_text_font(label_comp, &lv_font_montserrat_20, 0);
    lv_obj_set_style_text_color(label_comp, lv_color_hex(0xffffff), 0);
    lv_obj_align(label_comp, LV_ALIGN_CENTER, 0, 0);
    lv_label_set_text(label_comp, "0");
    
    // 返回提示符
    lv_obj_t *label_back = lv_label_create(icon_in_obj);
    lv_label_set_text(label_back, LV_SYMBOL_UP);
    lv_obj_set_style_text_color(label_back, lv_color_hex(0xffffff), 0);
    lv_obj_set_style_text_font(label_back, &lv_font_montserrat_20, 0);
    lv_obj_align(label_back, LV_ALIGN_BOTTOM_RIGHT, -20, -20);

    // 添加向上滑动退出功能
    lv_obj_add_event_cb(icon_in_obj, my_gesture_event_cb, LV_EVENT_GESTURE, 0);
    lv_obj_clear_flag(icon_in_obj, LV_OBJ_FLAG_GESTURE_BUBBLE);
    lv_obj_add_flag(icon_in_obj, LV_OBJ_FLAG_CLICKABLE);

    lvgl_port_unlock();

    my_lv_timer = lv_timer_create(comp_update_cb, 500, NULL);

    icon_flag = 11;
}